import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:kasax_demos2/commons/config_common.dart';
import 'dart:async';

import 'package:kasax_demos2/entry/entrys.dart';

/// Http请求管理器
class HttpManager {
  static final HttpManager _instance = HttpManager._internal();

  factory HttpManager() => _instance;

  static HttpManager getInstance() => _instance;

  HttpManager._internal();

  Future<List<Posts>> fetchPostsList() async {
    var responses = await http.get(Uri.parse(AppConsts.POSTS_URL));
    List<Posts> posts = <Posts>[];
    if (responses.statusCode == 200) {
      // 字符串转换为json
      // var dataMap = convert.jsonDecode(responses.body) as Map<String, dynamic>;
      var dataMap = convert.jsonDecode(responses.body);
      // 对象类型转换
      posts =
          dataMap['posts'].map<Posts>((item) => Posts.fromJson(item)).toList();
    }
    return posts;
  }
}
